<?php
/**
 *
 * PHP versions 5
 *
 * Copyright (C)
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software
 * Fundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * @author Bastian Schröder <bs@chemanet.de>
 * @version 2007-07-27
 * @package rail
 */

$mysqli = new mysqli( $_SESSION['mysql_host'], $_SESSION['mysql_user'], $_SESSION['mysql_passwd'], $_SESSION["mysql_db"] );
$rail = new rail_connector( $_SESSION["mysql_db"], $_SESSION["mysql_user"], $_SESSION["mysql_passwd"], $_SESSION["mysql_host"] );

$messages = array();
array_push( $messages, array( 'type' => 'info', 'messages' => 'MySQL Datenbank: ' . $_SESSION["mysql_db"] . '<br />rail Version: ' . $rail->get_root()->get_attribute('RAIL_VERSION') ) );
$_SESSION['cfg_pahToPHPrail'] = $cfg_pahToPHPrail;

// warn if database version unique current PHPrail version
if( substr($rail->get_root()->get_attribute('RAIL_VERSION'), 0, strpos($rail->get_root()->get_attribute('RAIL_VERSION'), '-')) != PHPrail_VERSION )
	array_push( $messages, array( 'type' => 'warn', 'messages' => 'Die Version der Datenbank stimmt nicht mit der des installierten PHPrail Interfaces überein.' ) );

// login
if( isset($_GET['rail_login']) ){
	unset($_SESSION['LOGIN']);
	header('Location: index.php?tpl=portal&LOGIN');
}
if( (isset($_GET['LOGIN']) || isset($_SESSION['LOGIN'])) ){
	if( isset($_SESSION['LOGIN']) ){
		try {
			if( !isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) )
				throw new rail_exception(null, 'username and password required.');

			$rail->login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);

			if( isset($_GET['rail_login']) )
				header('Location: index.php?tpl=portal');

			array_push( $messages, array( 'type' => 'info', 'messages' => 'Der Benutzer \'' . $_SERVER['PHP_AUTH_USER'] . '\' wurde am rail angemeldet.' ) );
		}
		catch( rail_exception $e ){
			unset($_SESSION['LOGIN']);
			if( isset($_SERVER['PHP_AUTH_USER']) && strlen($_SERVER['PHP_AUTH_USER']) > 0 )
				array_push( $messages, array( 'type' => 'warn', 'messages' => 'Der benutzer \'' . $_SERVER['PHP_AUTH_USER'] . '\' konnte nicht am rail angemeldet werden.' ) );
		}
	}
	else{
		$_SESSION['LOGIN'] = true;
		Header( 'WWW-Authenticate: Basic realm="carrier"' );
		Header( 'HTTP/1.0 401 Unauthorized' );

		die('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
  <meta http-equiv="content-language" content="de" />
  <meta http-equiv="content-type" content="text/html; charset=utf-8;" />
  <meta http-equiv="refresh" content="1"; URL="index.php?tpl=portal">
</head>
<body>
<script type="text/javascript">
  location.href = "index.php?tpl=portal";
</script>
</body>
</html>');
	}
}

// count rail objects adn rail_user objects
$num_obj = 0;
$data = $mysqli->query( "SELECT id FROM objects" );
while( $data_row = $data->fetch_assoc() )
	$num_obj ++;
$num_user = 0; #user
$data = $mysqli->query( "SELECT id FROM objects WHERE `type`='rail_user'" );
while( $data_row = $data->fetch_assoc() )
	$num_user ++;
array_push( $messages, array( 'type' => 'info', 'messages' => "Anzahl an rail Objekten: $num_obj<br />Benutzer im rail: $num_user" ) );

// get databases size
$data = $mysqli->query("SHOW TABLE STATUS FROM " . $_SESSION["mysql_db"] . " LIKE 'objects%'");
$size = 0;
while( $table = $data->fetch_assoc() )
	$size += $table['Data_length'];
array_push( $messages, array( 'type' => 'info', 'messages' => 'Größe der MySQL Datenbank: ' . format_size( $size ) ) );

// display all messages
$template->assign( 'messages', $messages );




//modules

// get all moduls and includes module/icon.png and module/load.js in portal.html
$modules = array();
$dirhandle = opendir( "modules/" );
while( $file = readdir($dirhandle) ) {
	if( substr( $file, 0, 1 ) != "." && $file != "folder.ini" && $file != "Thumbs.db" && $file != "README" )
	    $modules[] = array(
	    	"name"=>$file,
	    	"js"=>file_get_contents( "modules/" . $file . "/load.js" ),
	    	"icon"=>"modules/" . $file . "/icon.png"
	    );
}
function cmp_modules($a, $b){
	if( $a['name'] == 'railbrowser' )
		return -1;
	if( $a['name'] == 'users' && $b['name'] != 'railbrowser' )
		return -1;

	return 1;
}
usort($modules, 'cmp_modules');
closedir( $dirhandle );
$template->assign( "modules", $modules );




// some display vars
$template->assign( array(
	'rail_user' => $rail->get_user()->get_name()
));


/* ------------------------------------- functions ------------------------------------ */

function format_size( $size ) {
	if( $size >= 1073741824 )
		return round(($size / 1073741824), 2) . "GB";
	elseif( $size >= 1048576 )
		return round(($size / 1048576), 2) . "MB";
	elseif( $size >= 1024 )
		return round(($size / 1024), 2) . " KB";
	else
		return $size . " Byte";
}

?>